package com.paobuji.aidebao.myapplication.ui;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jaeger.library.StatusBarUtil;
import com.nightonke.jellytogglebutton.JellyToggleButton;
import com.nightonke.jellytogglebutton.State;
import com.paobuji.aidebao.myapplication.MainActivity;
import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.activity.BaseActivity;
import com.paobuji.aidebao.myapplication.tool.SharedPrefsUtil;
import com.paobuji.aidebao.myapplication.tool.SpUtil;
import com.paobuji.aidebao.myapplication.update.DeviceUtils;
import com.paobuji.aidebao.myapplication.update.UpdateManager;
import com.paobuji.aidebao.myapplication.view.CustomTitlebar;
import com.tencent.bugly.beta.Beta;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.paobuji.aidebao.myapplication.tool.Default.IS_CHINESE;

public class MySetActivity extends BaseActivity {

    @BindView(R.id.mainTitle)
    CustomTitlebar mainTitle;
    @BindView(R.id.screenSetBut)
    JellyToggleButton screenSetBut;
    @BindView(R.id.voiceSetVoice)
    JellyToggleButton voiceSetVoice;
    @BindView(R.id.updatePassText)
    TextView updatePassText;
    @BindView(R.id.updatePassImg)
    ImageView updatePassImg;
    @BindView(R.id.updatePassLayout)
    RelativeLayout updatePassLayout;
    @BindView(R.id.updateText)
    TextView updateText;
    @BindView(R.id.updateCode)
    TextView updateCode;
    @BindView(R.id.updateLayout)
    RelativeLayout updateLayout;
    @BindView(R.id.aboutText)
    TextView aboutText;
    @BindView(R.id.aboutImg)
    ImageView aboutImg;
    @BindView(R.id.aboutLayout)
    RelativeLayout aboutLayout;


    @BindView(R.id.languageText)
    TextView languageText;
    @BindView(R.id.languageImg)
    ImageView languageImg;
    @BindView(R.id.languageLayout)
    RelativeLayout languageLayout;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_set);
        ButterKnife.bind(this);
        //设置全透明
        StatusBarUtil.setTransparent(this);
        context = getApplicationContext();
        updateCode.setText(String.valueOf(DeviceUtils.getVersionName(context)));// 当前的版本号
        //标题栏事件监测
        mainTitle.setAction(new CustomTitlebar.TitleBarOnClickListener() {
            @Override
            public void performAction(View view) {
                switch (view.getId()) {
                    case R.id.iv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "左边图片按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.iv_right:
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_right:
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });


        //屏幕状态获取，如果没有记录，则设为1
        if (SharedPrefsUtil.getValue(context, "MySetting", "screen", 1) == 1) {
            SharedPrefsUtil.putValue(context, "MySetting", "screen", 1);        //屏幕常亮
            screenSetBut.setChecked(true);        //设置开关初始状态 开
        } else {
            screenSetBut.setChecked(false);        //设置开关初始状态 关
        }
        /*
        * 屏幕常亮设置
        */
        screenSetBut.setOnStateChangeListener(new JellyToggleButton.OnStateChangeListener() {
            @Override
            public void onStateChange(float process, State state, JellyToggleButton jtb) {
                // process - 当前动画进度，在[0, 1]之间
                // state   - JTB的当前状态，其值为State.LEFT，State.LEFT_TO_RIGHT，State.RIGHT和State.RIGHT_TO_LEFT其中之一
                // jtb     - JTB自身

                if (state.equals(State.LEFT)) {
                    SharedPrefsUtil.putValue(context, "MySetting", "screen", 0);        //屏幕不常亮
                } else if (state.equals(State.RIGHT)) {
                    SharedPrefsUtil.putValue(context, "MySetting", "screen", 1);        //屏幕常亮
                }
            }
        });

        //语音状态获取，如果没有记录，则设为1
        if (SharedPrefsUtil.getValue(context, "MySetting", "screen", 1) == 1) {
            SharedPrefsUtil.putValue(context, "MySetting", "voices", 1);        //语音开
            voiceSetVoice.setChecked(true);        //设置开关初始状态 开
        } else {
            voiceSetVoice.setChecked(false);        //设置开关初始状态 关
        }
        //语音开关设置
        voiceSetVoice.setOnStateChangeListener(new JellyToggleButton.OnStateChangeListener() {
            @Override
            public void onStateChange(float process, State state, JellyToggleButton jtb) {
                // process - 当前动画进度，在[0, 1]之间
                // state   - JTB的当前状态，其值为State.LEFT，State.LEFT_TO_RIGHT，State.RIGHT和State.RIGHT_TO_LEFT其中之一
                // jtb     - JTB自身

                if (state.equals(State.LEFT)) {
                    SharedPrefsUtil.putValue(context, "MySetting", "voices", 0);        //语音关
                } else if (state.equals(State.RIGHT)) {
                    SharedPrefsUtil.putValue(context, "MySetting", "voices", 1);        //语音开
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @OnClick({R.id.updatePassText, R.id.updatePassImg,
            R.id.updatePassLayout, R.id.updateText,
            R.id.updateCode, R.id.updateLayout,
            R.id.aboutText, R.id.aboutImg,
            R.id.aboutLayout,R.id.languageText,
            R.id.languageImg,R.id.languageLayout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.updatePassText:
                goToUpdatePass();
                break;
            case R.id.updatePassImg:
                goToUpdatePass();
                break;
            case R.id.updatePassLayout:
                goToUpdatePass();
                break;
            case R.id.updateText:
                updateCode();
                break;
            case R.id.updateCode:
                updateCode();
                break;
            case R.id.updateLayout:
                updateCode();
                break;
            case R.id.aboutText:
                goToAbout();
                break;
            case R.id.aboutImg:
                goToAbout();
                break;
            case R.id.aboutLayout:
                goToAbout();
                break;
            case R.id.languageText:
                setLanguage();
                break;
            case R.id.languageImg:
                setLanguage();
                break;
            case R.id.languageLayout:
                setLanguage();
                break;
        }
    }

    /**
     * 修改密码
     */
    public void goToUpdatePass(){
        startActivity(new Intent(getApplication(),UpdatePassActivity.class));// 这个线程的作用3秒后就是进入到你的主界面
    }

    /**
     * 监测版本更新
     */
    public void updateCode(){
        Log.i("jianbaopp","jianbaopp");
        Beta.checkUpgrade(false,false);
        Beta.checkUpgrade();
        /*UpdateManager updateManager = new UpdateManager(this);
        updateManager.checkUpdate();*/
    }

    /**
     * 关于
     */
    public void goToAbout(){


    }
      public void setLanguage(){
        if(IS_CHINESE){
            SpUtil.setInt(SpUtil.LANGUAGE, SpUtil.ENG);
            setLanguage("_EN");
            IS_CHINESE = false;
        }else{
            SpUtil.setInt(SpUtil.LANGUAGE, SpUtil.ZH);
            setLanguage("_ZH");
            IS_CHINESE = true;
        }
    }
    /**
     * Configuration是设置app配置的，Locale则是表示地理信息
     */
    private void setLanguage(String language) {
        //获取当前资源对象
        Resources resources = getResources();
        //获取设置对象
        Configuration configuration = resources.getConfiguration();
        //获取屏幕参数
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        //设置本地语言
        switch (language) {
            case "_ZH":
                configuration.locale = Locale.CHINA;//中文
                break;
            case "_EN":
                configuration.locale = Locale.ENGLISH;
                break;
            case "_JA":
                configuration.locale = Locale.JAPAN;
                break;
            case "_FR":
                configuration.locale = Locale.FRANCE;
                break;
            case "_ES":
                configuration.locale = new Locale("ES");
                break;
            case "_KO":
                //韩国
                configuration.locale = new Locale("KO");
                break;
            case "_IT":
                //意大利
                configuration.locale = new Locale("IT");
                break;
            case "_PT":
                //葡萄牙
                configuration.locale = new Locale("PT");
                break;
            case "_DE":
                //德国
                configuration.locale = Locale.GERMAN;
                break;
            case "_AR":
                //阿拉伯
                configuration.locale = new Locale("AR");
                break;
            case "_RU":
                //俄语
                configuration.locale = new Locale("RU");
                break;
        }
        resources.updateConfiguration(configuration, displayMetrics);

            //如果你的MainActivity是SingleTask需要这样重新启动Activity
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }


}
